package com.lepu.app.main.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.utils.BitmapLoadUtil;
import com.app.utils.Setting;
import com.core.lib.adapter.FragmentPageAdapter;
import com.core.lib.adapter.ViewPageAdapter;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.OnZoomImageClickCallBack;
import com.core.lib.utils.main.ImageUtilBase;
import com.core.lib.utils.photo.BitmapUtil;
import com.core.lib.utils.photo.UniversalImageLoadTool;
import com.core.lib.utils.viewpager.RotateDownPageTransformer;
import com.core.lib.utils.viewpager.ViewPagerCompat;
import com.core.lib.utils.viewpager.ViewPagerCompat.OnPageChangeListener;
import com.core.lib.widget.AnimationImageView;
import com.lepu.app.main.fragment.AppImageFragment;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewRightButtonListener;
import com.lepu.pasm.R;

public class AppImageBroswerActivity extends BaseFragmentActivity implements
		OnTopbarNewLeftLayoutListener, OnTopbarNewRightButtonListener {

	public ViewPagerCompat mViewPager;
	private LinearLayout mFocusIndicator;
	private TextView mBottomTipTextView;

	private String[] mUrlArray = null;
	private String[] mFilePathArray = null;

	private FragmentPageAdapter mAdapter = null;
	private ArrayList<Fragment> mFragmentList = new ArrayList<Fragment>();

	@Override
	public void onCreate(Bundle savedInstanceState) {

		mNeedStatusBar = false;
		mNeedSwipeBack = false;
		super.onCreate(savedInstanceState);
		setContentView(R.layout.app_image_broswer_activity);

		init();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	/**
	 * 添加页面动画，需要重写这个方法，
	 */
	@Override
	public void onBackPressed() {
		finish();
	}

	/**
	 * 初始化方法
	 */
	private void init() {

		boolean isShowTopbar = false;
		int index = 0;
		Bundle data = getIntent().getExtras();
		if (data != null && data.size() > 0) {
			isShowTopbar = data.getBoolean("isShowTopbar");
			mUrlArray = data.getStringArray("urlArray");
			mFilePathArray = data.getStringArray("filePathArray");
			index = data.getInt("index");
		}
		// topbar
		CustomTopBarNew topbar = (CustomTopBarNew) findViewById(R.id.topbar);

		if (isShowTopbar) {
			topbar.setTopbarTitle("");
			topbar.setRightText("删除");
			topbar.setonTopbarNewLeftLayoutListener(this);
			topbar.setOnTopbarNewRightButtonListener(this);
		} else {
			topbar.setVisibility(View.GONE);
		}

		mViewPager = (ViewPagerCompat) findViewById(R.id.viewPager);
		mViewPager.setPageTransformer(true, new RotateDownPageTransformer());
		mFocusIndicator = (LinearLayout) findViewById(R.id.focusIndicator);
		mBottomTipTextView = (TextView) findViewById(R.id.bottomTipTextView);

//		int count = 0;
//		if (mUrlArray != null && mUrlArray.length > 0) {
//			count = mUrlArray.length;
//		} else {
//			if (mFilePathArray != null && mFilePathArray.length > 0) {
//				count = mFilePathArray.length;
//			}
//		}
//
//		for (int i = 0; i < count; i++) {
//			View v = new View(this);
//			v.setBackgroundResource(R.drawable.app_point_bg);
//			LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
//					12, 12);
//			layoutParams.setMargins(4, 4, 4, 4);
//			v.setEnabled(false);
//
//			if (i == index) {
//				v.setEnabled(true);
//			}
//			mFocusIndicator.addView(v, layoutParams);
//		}

		addView();

		mAdapter = new FragmentPageAdapter(getSupportFragmentManager(),mFragmentList, null, null);
		mViewPager.setAdapter(mAdapter);
		mViewPager.setOnPageChangeListener(new MyOnPageChangeListener());
		mViewPager.setCurrentItem(index);

		updateBottomTip(index);
	}

	private void addView() {

		if (mUrlArray != null && mUrlArray.length > 0) {
			for (String url : mUrlArray) {

				AppImageFragment fragment = new AppImageFragment();

				Bundle data = new Bundle();
				data.putBoolean("is_net",true);
				data.putString("url", url);
				fragment.setArguments(data);

				mFragmentList.add(fragment);

//				AnimationImageView imageView = createImageView();
//
//				// load image
//				String imageUrl = "";
//				if (url.startsWith("http")) {
//					imageUrl = Setting.getImageUrl(url);
//				} else {
//					imageUrl = ImageUtilBase.getFileImageUrl(url);
//				}
//
//				UniversalImageLoadTool.disPlay(imageUrl, imageView,
//						R.drawable.default_icon_small);
//
//				mViewList.add(imageView);
			}
		} else {
			if (mFilePathArray != null && mFilePathArray.length > 0) {
				for (String filePath : mFilePathArray) {

					AppImageFragment fragment = new AppImageFragment();
					Bundle data = new Bundle();
					data.putBoolean("is_net", false);
					data.putString("filePath", filePath);
					fragment.setArguments(data);
					mFragmentList.add(fragment);

//					AnimationImageView imageView = createImageView();
//					Bitmap bitmap = ImageUtilBase.getBitmap(filePath,480,800);
//					imageView.setImageBitmap(BitmapUtil.reviewPicRotate(bitmap, filePath));
//
//					mViewList.add(imageView);
				}
			}
		}
	}

	private AnimationImageView createImageView()
	{
		final AnimationImageView imageView = new AnimationImageView(
				this);
		imageView.setLayoutParams(new ViewGroup.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.MATCH_PARENT));

		imageView.setAdjustViewBounds(true);
		imageView.setScaleType(ScaleType.FIT_CENTER);
		imageView
				.setClickCallBack(new OnZoomImageClickCallBack() {

					@Override
					public void OnSingleTapConfirmed(PointF point) {
						finish();
					}

					@Override
					public void OnLongClickResponse(PointF point) {

					}

					@Override
					public void OnDoubleClickResponse(PointF point) {
						float magfactor = 1.6f;
						float float_epsilon = 1e-3f;
						float viewwidth = imageView.getWidth();
						float viewheight = imageView.getHeight();
						float imagewidth = imageView.getDrawable()
								.getBounds().width();
						float imageheight = imageView.getDrawable()
								.getBounds().height();

						Matrix matrix = new Matrix();
						matrix.set(imageView.getImageMatrix());
						float[] values = new float[9];
						matrix.getValues(values);

						float ratiox = viewwidth / imagewidth;
						float ratioy = viewheight / imageheight;
						float minratio = ratiox < ratioy ? ratiox
								: ratioy;
						float transx, transy;
						// 回原图
						if (values[0] > (minratio + float_epsilon)) {
							if (ratiox < ratioy) {
								transx = 0;
								transy = (viewheight - ratiox
										* imageheight) / 2;
							} else {
								transx = (viewwidth - ratioy
										* imagewidth) / 2;
								transy = 0;
							}
							values[0] = values[4] = minratio;
							values[2] = transx;
							values[5] = transy;
							matrix.setValues(values);
							imageView.EnableAnimation(
									imageView.getImageMatrix(),
									matrix, 200);
						} else {// 放大
							matrix.postScale(magfactor, magfactor,
									point.x, point.y);
							imageView.EnableAnimation(
									imageView.getImageMatrix(),
									matrix, 200);
						}
					}
				});
		return imageView;
	}

	private void updateBottomTip(int index)
	{
		mBottomTipTextView.setText(String.format("%d / %d",index+1,mFragmentList.size()));
	}

	public class MyOnPageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageSelected(int position) {
//			for (int i = 0; i < mFocusIndicator.getChildCount(); i++) {
//				mFocusIndicator.getChildAt(i).setEnabled(false);
//			}
//
//			mFocusIndicator.getChildAt(position).setEnabled(true);

			updateBottomTip(position);
		}

		@Override
		public void onPageScrolled(int position, float positionOffset,
				int positionOffsetPixels) {
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}
	}

	@Override
	public void onTopbarLeftLayoutSelected() {
		finish();
	}

	@Override
	public void onTopbarRightButtonSelected() {

		// mViewList.remove(mViewPager.getCurrentItem());
		// mAdapter.notifyDataSetChanged();

		Intent intent = new Intent();
		intent.putExtra("index", mViewPager.getCurrentItem());
		setResult(Activity.RESULT_OK, intent);

		finish();
	}

}
